package com.study.d012Components.treeview;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeItem.TreeModificationEvent;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/** 自定义树， 编辑，提交
*@description
*@author xtayhame
*date 2020年3月15日
*version
*since v1.0
*/
public class TreeViewCustomizeMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane an = new AnchorPane();
		Button b1 = new Button("点击");

		TreeView<String> treeView = new TreeView<String>();
		treeView.setPrefSize(200, 300);

		TreeItem<String> root = new TreeItem<String>("中国");

		TreeItem<String> item_gd = new TreeItem<String>("广东省");
		TreeItem<String> item_gd_1 = new TreeItem<String>("广州市");
		TreeItem<String> item_gd_2 = new TreeItem<String>("深圳市");
		TreeItem<String> item_gd_3 = new TreeItem<String>("佛山市");
		item_gd.getChildren().addAll(item_gd_1,item_gd_2,item_gd_3);


		TreeItem<String> item_hn = new TreeItem<String>("河南省");
		TreeItem<String> item_hn_1 = new TreeItem<String>("南阳市");
		TreeItem<String> item_hn_2 = new TreeItem<String>("商丘市");
		TreeItem<String> item_hn_3 = new TreeItem<String>("驻马店市");
		item_hn.getChildren().addAll(item_hn_1,item_hn_2,item_hn_3);

		root.getChildren().addAll(item_gd,item_hn);

		treeView.setRoot(root);


		an.getChildren().addAll(b1,treeView);
		AnchorPane.setTopAnchor(treeView, 30.0);
		AnchorPane.setLeftAnchor(treeView, 50.0);
		stage.setScene(new Scene(an, 400, 500));
		stage.show();


		treeView.setEditable(true);

		treeView.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {

			@Override
			public TreeCell<String> call(TreeView<String> param) {
				TreeCell cell = new TreeCell<String>(){

					@Override
					protected void updateItem(String item, boolean empty) {
						super.updateItem(item, empty);

						if(!empty && item !=null){
							HBox hBox = new HBox();
							Label label = new Label(item);
							hBox.getChildren().add(label);
							this.setGraphic(hBox);

							//添加图标
							if(this.getTreeItem().isExpanded()){
								//设置图标 替换为+号
								ImageView iv = new ImageView("icon/plus.jpg");
								iv.setPreserveRatio(true);
								iv.setFitHeight(10);
								this.setDisclosureNode(iv);
							}else{
								//设置图标 替换为-号
								ImageView iv = new ImageView("icon/sub.jpg");
								iv.setPreserveRatio(true);
								iv.setFitHeight(15);
								this.setDisclosureNode(iv);
							}

						}else{
							//注意：这里要设置一下，如果前面设置了setGraphic(),则这里要重新设置为空，
							//如果前面设置了setDisclosureNode(),则这里也要设置为空
							this.setGraphic(null);
							this.setDisclosureNode(null);
						}




					}

					//开始编辑
					@Override
					public void startEdit() {
						super.startEdit();
						HBox hBox = new HBox();
						TextField textField = new TextField(this.getItem());
						hBox.getChildren().add(textField);
						this.setGraphic(hBox);

						TreeCell tempCell=this;

						textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
							@Override
							public void handle(KeyEvent event) {
								if(event.getCode()==KeyCode.ENTER){
									//如果是Enter键，则提交
									String newValue= textField.getText();
									tempCell.commitEdit(newValue);
								}

							}
						});
					}

					//取消编辑
					@Override
					public void cancelEdit() {
						super.cancelEdit();

						HBox hBox = new HBox();
						Label label = new Label(this.getItem());
						hBox.getChildren().add(label);
						this.setGraphic(hBox);
					}


				};

				return cell;
			}
		});

	}
}
